﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class EditProductCategory : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            ProductCategoryBA productCategoryBA = new ProductCategoryBA();

            if (Request["id"] != null && StringUtil.IsPositiveNumber(Request["id"]))
            {
                int updateID = Convert.ToInt32(Request["id"]);
                ProductCategoryTO productCategoryTO = productCategoryBA.GetProductCategoryByID(updateID);
                Attributes.Add("productCategory", productCategoryTO);
            }
        }

        public void Update(HttpRequest request, HttpResponse response)
        {

            ProductCategoryBA productCategoryBA = new ProductCategoryBA();

            if (Request["id"] == null || Request["id"].Equals("") || !StringUtil.IsPositiveNumber(Request["id"]))
            {
                this.error("Category is invalid");
                this.isLoad = true;
                return;
            }
            int ID = Convert.ToInt32(Request["id"]);
            ProductCategoryTO productCategoryTO = productCategoryBA.GetProductCategoryByID(ID);
            if (productCategoryTO == null)
            {
                this.error("Category is invalid");
                this.isLoad = true;
                return;
            }
            
            
            String name = Request["Name"];
            int status = Convert.ToInt32(Request["Status"]);
            if (name == null || "".Equals(name))
            {
                this.error("Name can not be empty");
                this.isLoad = true;
            }
            else
            {
                productCategoryTO.Name = name;
                productCategoryTO.UpdateDate = DateTime.Now;
                productCategoryTO.Status = status;
                productCategoryTO.ID = ID;
                Boolean stt = productCategoryBA.UpdateProductCategory(productCategoryTO);
                if (stt)
                {
                    Response.Redirect("/ViewProductCategory.aspx");
                }
                else
                {
                    this.error("Update failed");
                    this.isLoad = true;
                }
            }
        }
    }
}